<template>
  <div class="register">
    <!-- 背景装饰 -->
    <div class="register-bg-wrapper">
      <div class="bg-particles"></div>
      <div class="bg-gradient"></div>
    </div>
    
    <!-- 注册表单容器 -->
    <div class="register-container">
      <div class="register-form-wrapper">
        <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="register-form">
          <div class="form-header">
            <h2 class="title">{{ title }}</h2>
            <p class="subtitle">创建您的新账户，开始您的旅程</p>
          </div>
          <el-form-item prop="username" class="form-item">
            <label class="form-label">用户名</label>
            <el-input 
              v-model="registerForm.username" 
              type="text" 
              size="large" 
              auto-complete="off" 
              placeholder="请输入用户名（2-20个字符）"
              class="modern-input"
            >
              <template #prefix><svg-icon icon-class="user" class="input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password" class="form-item">
            <label class="form-label">密码</label>
            <el-input
              v-model="registerForm.password"
              type="password"
              size="large" 
              auto-complete="off"
              placeholder="请输入密码（5-20个字符）"
              class="modern-input"
              @keyup.enter="handleRegister"
            >
              <template #prefix><svg-icon icon-class="password" class="input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword" class="form-item">
            <label class="form-label">确认密码</label>
            <el-input
              v-model="registerForm.confirmPassword"
              type="password"
              size="large" 
              auto-complete="off"
              placeholder="请再次输入密码"
              class="modern-input"
              @keyup.enter="handleRegister"
            >
              <template #prefix><svg-icon icon-class="password" class="input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="code" v-if="captchaEnabled" class="form-item captcha-item">
            <label class="form-label">验证码</label>
            <div class="captcha-wrapper">
              <el-input
                size="large" 
                v-model="registerForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                class="modern-input captcha-input"
                @keyup.enter="handleRegister"
              >
                <template #prefix><svg-icon icon-class="validCode" class="input-icon" /></template>
              </el-input>
              <div class="captcha-image-wrapper">
                <img :src="codeUrl" @click="getCode" class="captcha-image" title="点击刷新验证码"/>
              </div>
            </div>
          </el-form-item>
          <el-form-item class="submit-item">
            <el-button
              :loading="loading"
              size="large" 
              type="primary"
              class="register-button"
              @click.prevent="handleRegister"
            >
              <span v-if="!loading">立即注册</span>
              <span v-else>注册中...</span>
            </el-button>
          </el-form-item>
          
          <div class="form-footer">
            <span class="footer-text">已有账户？</span>
            <router-link class="login-link" :to="'/login'">立即登录</router-link>
          </div>
        </el-form>
      </div>
    </div>
    
    <!-- 底部版权信息 -->
    <div class="register-footer">
      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ElMessageBox } from "element-plus"
import { getCodeImg, register } from "@/api/login"

const title = import.meta.env.VITE_APP_TITLE
const router = useRouter()
const { proxy } = getCurrentInstance()

const registerForm = ref({
  username: "",
  password: "",
  confirmPassword: "",
  code: "",
  uuid: ""
})

const equalToPassword = (rule, value, callback) => {
  if (registerForm.value.password !== value) {
    callback(new Error("两次输入的密码不一致"))
  } else {
    callback()
  }
}

const registerRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" },
    { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" }
  ],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}

const codeUrl = ref("")
const loading = ref(false)
const captchaEnabled = ref(true)

function handleRegister() {
  proxy.$refs.registerRef.validate(valid => {
    if (valid) {
      loading.value = true
      register(registerForm.value).then(res => {
        const username = registerForm.value.username
        ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", "系统提示", {
          dangerouslyUseHTMLString: true,
          type: "success",
        }).then(() => {
          router.push("/login")
        }).catch(() => {})
      }).catch(() => {
        loading.value = false
        if (captchaEnabled) {
          getCode()
        }
      })
    }
  })
}

function getCode() {
  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img
      registerForm.value.uuid = res.uuid
    }
  })
}

getCode()
</script>

<style lang='scss' scoped>
// 全屏注册容器
.register {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

// 背景装饰
.register-bg-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

// 渐变背景(与登录页面略有不同的颜色)
.bg-gradient {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    135deg,
    #764ba2 0%,
    #667eea 25%,
    #f5576c 50%,
    #f093fb 75%,
    #4facfe 100%
  );
  background-size: 400% 400%;
  animation: gradientShift 15s ease infinite;
}

// 粒子效果背景
.bg-particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(circle at 20% 80%, rgba(198, 119, 120, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(119, 198, 255, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(198, 120, 198, 0.3) 0%, transparent 50%);
  animation: particleFloat 20s ease-in-out infinite;
}

// 渐变动画
@keyframes gradientShift {
  0%, 100% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
}

// 粒子浮动动画
@keyframes particleFloat {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  33% {
    transform: translate(30px, -30px) rotate(120deg);
  }
  66% {
    transform: translate(-20px, 20px) rotate(240deg);
  }
}

// 注册容器
.register-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 440px;
  margin: 0 20px;
}

// 注册表单包装器
.register-form-wrapper {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.1),
    0 2px 16px rgba(0, 0, 0, 0.05);
  animation: slideUp 0.8s ease-out;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
    animation: shimmer 2s ease-in-out infinite;
  }
}

// 滑入动画
@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// 光影效果动画
@keyframes shimmer {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

// 表单头部
.form-header {
  text-align: center;
  margin-bottom: 32px;
  
  .title {
    font-size: 28px;
    font-weight: 700;
    color: #ffffff;
    margin: 0 0 8px 0;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .subtitle {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
    margin: 0;
    font-weight: 400;
  }
}

// 表单项
.form-item {
  margin-bottom: 24px;
  
  .form-label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.9);
    margin-bottom: 8px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  }
}

// 现代化输入框
.modern-input {
  :deep(.el-input__wrapper) {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    box-shadow: none;
    backdrop-filter: blur(10px);
    transition: all 0.3s ease;
    padding: 0 16px;
    
    &:hover {
      border-color: rgba(255, 255, 255, 0.3);
      background: rgba(255, 255, 255, 0.15);
    }
    
    &.is-focus {
      border-color: rgba(255, 255, 255, 0.5);
      background: rgba(255, 255, 255, 0.2);
      box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.1);
    }
  }
  
  :deep(.el-input__inner) {
    color: #ffffff;
    background: transparent;
    border: none;
    font-size: 15px;
    height: 48px;
    
    &::placeholder {
      color: rgba(255, 255, 255, 0.6);
    }
  }
  
  .input-icon {
    color: rgba(255, 255, 255, 0.7);
    width: 16px;
    height: 16px;
  }
}

// 验证码相关
.captcha-item {
  .captcha-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-end;
  }
  
  .captcha-input {
    flex: 1;
  }
  
  .captcha-image-wrapper {
    width: 120px;
    height: 48px;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    transition: all 0.3s ease;
    
    &:hover {
      border-color: rgba(255, 255, 255, 0.3);
      transform: scale(1.02);
    }
  }
  
  .captcha-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
      opacity: 0.8;
    }
  }
}

// 提交按钮
.submit-item {
  margin-bottom: 24px;
  
  .register-button {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 12px;
    background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
    border: none;
    box-shadow: 0 4px 16px rgba(118, 75, 162, 0.4);
    transition: all 0.3s ease;
    text-transform: none;
    letter-spacing: 0.5px;
    
    &:hover:not(.is-loading) {
      transform: translateY(-2px);
      box-shadow: 0 6px 20px rgba(118, 75, 162, 0.5);
      background: linear-gradient(135deg, #8659b2 0%, #7289ea 100%);
    }
    
    &:active:not(.is-loading) {
      transform: translateY(0);
    }
    
    &.is-loading {
      opacity: 0.8;
    }
  }
}

// 表单底部
.form-footer {
  text-align: center;
  
  .footer-text {
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    margin-right: 8px;
  }
  
  .login-link {
    color: #ffffff;
    font-weight: 600;
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: all 0.3s ease;
    
    &:hover {
      border-bottom-color: rgba(255, 255, 255, 0.5);
    }
  }
}

// 底部版权
.register-footer {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  color: rgba(255, 255, 255, 0.6);
  font-size: 12px;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

// 响应式设计
@media (max-width: 480px) {
  .register-container {
    margin: 0 16px;
  }
  
  .register-form-wrapper {
    padding: 32px 24px;
  }
  
  .form-header .title {
    font-size: 24px;
  }
  
  .captcha-item .captcha-wrapper {
    flex-direction: column;
    
    .captcha-image-wrapper {
      width: 100%;
      height: 40px;
    }
  }
}
</style>

